import userAgreementPopup from '@/components/user-agreement-popup/user-agreement-popup.vue' //免责
import md5 from '@/utils/blueimp-md5.js'
export default {
  components: {
    userAgreementPopup, //弹出框组件
  },
  data() {
    return {
      mobileName: '', //输入的手机号
      invitationCode: '', //输入的验证码
      type: 'number', //input输入格式
      isAgree: false, //false未选择  true我同意选择
      olduser: false, //false为老用户，true为新用户
      yqsuccess: false, //邀请成功
      yqfail: false, //邀请失败
      deletebtn: false, //邀请码清楚按钮
      nextStepstat: false, //false下一步灰色，true下一步点亮
      passwordSMSLogin: '密码登录', //密码短信登录切换
      pasworcontrol: false, //密码登录控制

      passwordPlaceholder: '请输入密码', //input默认提示
      showpasswordPlaceholder: false, //密码小标题
      showPasswordClearIcon: false, //密码小叉叉
      showPassword: true, //隐藏显示密码
      Password: '', //input输入的值
      eye: 'eye-off', //显示隐藏的眼睛图标
      isFocus: false,

      guidePages: false, //隐藏显示登录
      historyInfo: null, //历史手机号

      scrollTop: 0,
      old: {
        scrollTop: 0
      },

      seconds: 60,
      // 倒计时
      tips: '',
      weixinList: '',
	  platform: '', //设备平台信息
    }
  },
  computed: {
    isDisabled() {
      if (this.mobileName.length === 11) {
        return false
      }
      return true
    },
    btnText() {
      if (this.quickUser && this.mobileName === this.quickUser) {
        return '下一步'
      }
      return '获取验证码'
    },
  },
  onLoad(e) {
    this.weixinList = JSON.parse(e.weixinList)
    this.loadExecution() //调用登录页

    // #ifdef APP-PLUS
    //获取历史登录记录-imei
    const _this = this
    plus.device.getInfo({
      success: function(res) {
        //因为ios没有imei,所以取uuid
        const imei = res.imei.length < 2 ? res.uuid : res.imei
        _this.$store.commit('setImei', imei)
        _this.verifyMobile(imei)
        // console.log('获取到得imei',imei)
      },
      complete: function(err) {},
    })
    // #endif

    if (e.number) {
      this.mobileName = e.number
      this.showClearIcon = true
      this.showSmalltitle()
    }

    // 获取设备信息
    const systemInfo = uni.getSystemInfoSync()
   let plat = ''
       // #ifdef APP-PLUS
       plat = '_APP'
       //  #endif
       // #ifdef H5
       plat = '_H5'
       //  #endif
       // #ifdef MP-WEIXIN
       plat = '_WEIXIN'
       //  #endif
	   this.platform = systemInfo.platform
	       if(systemInfo.brand){
	         this.platform = this.platform + '_' + systemInfo.brand
	       }
	       if(systemInfo.model){
	         this.platform = this.platform  + '_' + systemInfo.model
	       }
	       this.platform = this.platform + plat
  },
  //监听默认返回事件
  onBackPress(e) {
    return true
  },
  methods: {
    md5,
    //倒计时
    codeChange(text) {
      // console.log(text)
      this.tips = text
    },
    end() {
      // this.$u.toast('倒计时结束')
    },
    start() {
      this.$u.toast('已发送')
    },
    tipsbtns(ss) {
      if (ss === '重新获取验证码') {
        //重新获取验证码
        this.sendCode()
      } else if (ss === '获取验证码') {
        this.sendCode()
      } else {
        // console.log(234)
      }
    },
    //发送验证码
    async sendCode(type) {
      try {
        if (this.mobileName) {
          uni.showToast({
            title: '请输入手机号',
            icon: 'none'
          })
        }
        const data = await this.$api.user.SendVerficationCode({
          mobile: this.mobileName
        })
        this.Password = '' //清空验证码
        // this.isDisabled = true //灰色下一步
        this.overtime = false //过期
        uni.hideLoading()
        uni.showToast({
          title: '短信发送成功',
          icon: 'none'
        })

        // 模拟向后端请求验证码
        uni.showLoading({
          title: '正在获取验证码'
        })
        setTimeout(() => {
          uni.hideLoading()
          // 这里此提示会被this.start()方法中的提示覆盖
          this.$u.toast('验证码已发送')
          // 通知验证码组件内部开始倒计时
          this.$refs.uCode.start()
        }, 2000)
        // this.getCode()
      } catch (err) {
        console.log(err)
        uni.hideLoading()
      }
    },

    scroll: function(e) {
      this.old.scrollTop = e.detail.scrollTop
    },

    loadExecution: function() {
      /**
       * 获取本地存储中launchFlag的值
       * 若存在，说明不是首次启动，直接进入首页；
       * 若不存在，说明是首次启动，进入引导页；
       */
      try {
        // 获取本地存储中launchFlag标识
        const value = uni.getStorageSync('launchFlag')
        if (value) {
          // launchFlag=true直接跳转到首页
          uni.switchTab({
            url: '/pages/index/index'
          })
        } else {
          // launchFlag!=true显示引导页
          this.guidePages = true
        }
      } catch (e) {
        // error
        uni.setStorage({
          key: 'launchFlag',
          data: true,
          success: function() {
            console.log('error时存储launchFlag')
          }
        })
        this.guidePages = true
      }
      return
      uni.switchTab({
        url: '/pages/index/index'
      })
    },
    //实时监控--手机号--input输入的值
    moblieInput(event) {
      // 手机号不足11位,邀请码不足6位
      if (this.mobileName.length !== 11) {
        this.nextStepstat = false
        this.olduser = false //显示邀请码
        this.isAgree = false
      } else {
        // this.nextStepstat = true
        this.isFocus = false
        this.CheckAccountExsist(this.mobileName)
      }
    },
    //实时监控--邀请码--input输入的值
    clearInput(event) {
      this.invitationCode = event //input输入的内容

      if (this.invitationCode.length !== 6) {
        this.nextStepstat = false
        this.olduser = false //显示邀请码
        this.isAgree = false
      } else {
        this.nextStepstat = true
      }
    },
    //当input框获取焦点的时候，显示小标题，隐藏默认提示文字
    showSmalltitle() {
      this.placeholder = '' //input默认提示字
      this.isFocus = true

      if (this.mobileName.length === 11) {
        this.isFocus = false
      }
    },
    //当input框失去焦点的时候，隐藏小标题，显示默认提示文字
    hideSmalltitle() {
      if (this.mobileName === '') {
        this.placeholder = '请输入手机号' //input默认提示字
      }
      this.isFocus = false
    },
    //验证用户是否存在  true是老用户，fasle是新用户
    async CheckAccountExsist(data) {
      try {
        const res = await this.$api.user.CheckAccountExsist({
          mobile: data
        })
        this.olduser = res.data
        this.isAgree = res.data
      } catch (e) {}
    },
    //下一步
    nextStepClick() {
      this.LoginWithWeChatToken()
    },
    //微信登录新用户登录接口
    async LoginWithWeChatToken() {
      var type = this.weixinList
      console.log('1212', type.authResult.openid)
      console.log('1313', type.authResult.access_token)
      console.log('1313', this.mobileName)
      console.log('1313', this.invitationCode)
      console.log('1313', this.$store.state.imei)
      try {
        const res = await this.$api.user.LoginWithWeChatToken({
          mobile: this.mobileName,
          loginCode: this.invitationCode,
          imei: this.$store.state.imei,
          token: type.authResult.access_token,
          openId: type.authResult.openid,
		  login_Platform: this.platform, //设备平台信息-仅注册需要
        })
        console.log(res)
        this.loginSuccess(res.data) //保存
        uni.setStorage({
          key: 'launchFlag',
          data: true,
        })
        uni.switchTab({
          url: '/pages/index/index'
        })
      } catch (e) {}
    },

    async passwdlogn() {
      const md5Pwd = this.md5(this.Password)
      try {
        const res = await this.$api.user.Login({
          mobile: this.mobileName, //用户名
          loginCode: md5Pwd, //	验证码/密码
          inviteCode: this.invitationCode, //邀请码
          imei: this.$store.state.imei, //IMEI
          isMobile: false //true、手机号+验证码模式登入 fasle、账号+密码模式登入）
        })
        if (res.code === 200) {
          this.loginSuccess(res.data) //保存
          uni.setStorage({
            key: 'launchFlag',
            data: true,
          })
          uni.reLaunch({
            url: '/pages_logins/passwordLogin/passwordLogin?type=' + res.data.isNew
          })
        } else {
          this.Password = ''
        }
      } catch (err) {
        console.log('捕获错误', err)
      }
    },

    //保存登录信息
    loginSuccess(param) {
      // 登录信息-vuex
      this.$store.commit('setModel', param)

      //token存储-缓存
      uni.setStorage({
        key: 'token',
        data: param.token,
      })
      //用户信息-缓存
      uni.setStorage({
        key: 'Ztc_C_USER_INFO',
        data: param,
      })
    },
    //勾选免责
    Agree() {
      this.isAgree = !(this.isAgree)
    },
    //历史号码点击
    selHistory(data) {
      // debugger
      console.log(123)
      this.mobileName = data
      this.CheckAccountExsist(data)
      this.isFocus = false
      this.nextStepstat = true //下一步点亮
    },
    //同意免责
    agreepopup(type) { //type 0不同意 1同意
      if (type === 0) {
        this.isAgree = false
        this.showPopup = false //关闭免责弹框
        // this.getCode()
      } else {
        this.isAgree = true

        // 点击同意跳到输入验证码的页面
        var isMobile = true //true、手机号+验证码模式登入 fasle、账号+密码模式登入）
        uni.navigateTo({
          url: '/pages_logins/enterCode/enterCode?mobileName=' + this.mobileName + '&isMobile=' + isMobile +
            '&invitationCode=' + this.invitationCode
        })
        this.showPopup = false //关闭免责弹框
      }
    },
    //跳转协议
    gouserAgreementPage() {
      uni.navigateTo({
        url: '/pages_logins/userAgreementPopup/userAgreementPopup'
      })
    },
    async verifyMobile(data) {
      try {
        const res = await this.$api.user.GetAccountsByIMEI({
          imei: data,
        })
        if (res.data.length > 0) {
          this.historyInfo = res.data //赋值历史手机号
          if (res.data.length === 1) {
            this.quickUser = res.data[0]
            this.mobileName = this.mobileName ? this.mobileName : res.data[0].mobile
            this.showClearIcon = true //显示小叉叉
            this.placeholder = '请输入手机号'
            this.olduser = false
          } else if (res.data.length > 1) {
            this.olduser = false
          }
          this.nextStepstat = true
        }
      } catch (e) {}
    },

  },
}
